<?php
/**
 * Created by PhpStorm.
 *
 * Author : Traveler.
 * Time   : 2019/12/27 14:13
 * Company: Inner Mongolia Zhengzhi Network Technology Co., Ltd
 */

namespace app\admin\model;


use app\admin\BaseModel;

class Icafe extends BaseModel
{
    /**
     * 数据表名称
     * @var string
     */
    protected $table = 'ibms_icafe';

    /**
     * 查询字段
     * @var array
     */
    protected $query = [
        'id'        => '=',
        'name'      => 'like',
        'contact'   => 'like',
        'cellphone' => 'like',
        'cate_id'   => '=',
        'status'    => '=',
    ];

    /**
     * (静态)获取场所统计
     * @return int
     */
    public static function getIcafeCount()
    {
        return static::where('delete_time', 0)->count();
    }

    public static function getIcafeEchart(string $mode)
    {
        $data = [];
        switch ($mode) {
            case 'area':
                $series = Area::where('pid', 150000)->select();
                foreach ($series as $city) {
                    $data[] = [
                        'name' => $city['name'],
                        'value' => static::where('city', $city['id'])->count()
                    ];
                }
                break;
            case 'cate':
                $series = IcafeCate::where('delete_time', 0)->select();
                foreach ($series as $cate) {
                    $data[] = [
                        'name' => $cate['name'],
                        'value' => static::where('cate_id', $cate['id'])->count(),
                        'itemStyle' => ['color' => $cate['bgcolor']]
                    ];
                }
                break;
            case 'hhht':
                $series = Area::where('pid', 150100)->select();
                foreach ($series as $district) {
                    $data[] = [
                        'name' => $district['name'],
                        'value' => static::where('district', $district['id'])->count()
                    ];
                }
                break;
            default:
                break;
        }

        return $data;
    }

    /**
     * (静态)获取表格渲染数据
     * @param array $param     请求参数
     * @param bool  $isdeleted 删除标记
     * @return array
     */
    public static function getIcafeTable(array $param = [], bool $isdeleted = false)
    {
        $model  = new static;
        $with   = ['icafeCate', 'icafeAdsl', 'icafeCnk', 'icafeMirror', 'icafeWan'];
        $where  = [['delete_time', $isdeleted ? '>' : '=', 0]];
        $filter = $model->filterParam($param, $where);

        return [
            'data'  => $model->where($filter['where'])->with($with)->append(['areaName'])
                ->order('id')->page($filter['page'], $filter['limit'])->select(),
            'count' => $model->where($filter['where'])->count()
        ];
    }

    /**
     * 模型关联：场所分类
     * @return \think\model\relation\BelongsTo
     */
    public function icafeCate()
    {
        return $this->belongsTo(IcafeCate::class, 'cate_id');
    }

    /**
     * 模型关联：外网：ADSL
     * @return \think\model\relation\HasMany
     */
    public function icafeAdsl()
    {
        return $this->hasMany(IcafeAdsl::class);
    }

    /**
     * 模型关联：净网先锋
     * @return \think\model\relation\HasOne
     */

    public function icafeCnk()
    {
        return $this->hasOne(IcafeCnk::class);
    }

    /**
     * 模型关联：端口镜像
     * @return \think\model\relation\HasOne
     */
    public function icafeMirror()
    {
        return $this->hasOne(IcafeMirror::class);
    }

    /**
     * 模型关联：外网：专线
     * @return \think\model\relation\HasMany
     */
    public function icafeWan()
    {
        return $this->hasMany(IcafeWan::class);
    }

    /**
     * 获取器：区域代码名称
     * @param mixed $value
     * @param array $data  当前的所有数据数组
     * @return array
     */
    public function getAreaNameAttr($value, $data)
    {
        $provinceName = Area::find($data['province'])['name'];
        $cityName     = Area::find($data['city'])['name'];
        $districtName = Area::find($data['district'])['name'];

        return compact('provinceName', 'cityName', 'districtName');
    }
}